package com.zjzywl.iotSendthings.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zjzywl.iotSendthings.entity.JDWPBGXX;
import com.zjzywl.iotSendthings.repository.ProvideInformationRepository;
import com.zjzywl.iotSendthings.util.PageBean;
import com.zjzywl.iotSendthings.util.WebResult;

@RestController
@RequestMapping(value = "/history")
public class HistoryController {
	@Autowired
	private ProvideInformationRepository jdwpgxx;
	
	@RequestMapping(value = "/pageNumber")
	public WebResult pageNumber(HttpServletRequest request) {
		
		List<JDWPBGXX> clist = jdwpgxx.findByBz();
		// 初始化PageBean对象
		PageBean pagebean = new PageBean(clist.size());
		// 获得分页大小
		int pagesize = pagebean.getPageSize();
		System.out.println("历史记录总页码："+(clist.size()+pagesize-1)/pagesize);
		return WebResult.OK((clist.size()+pagesize-1)/pagesize);

	}
	
	@RequestMapping(value = "/approve")
	public WebResult approveHistory(HttpServletRequest request) {
		int page = Integer.parseInt(request.getParameter("page")) ;
		System.out.println("前台传来的页码:"+page);
		List<JDWPBGXX> clist = jdwpgxx.findByBz();
		PageBean pagebean = new PageBean(clist.size());// 初始化PageBean对象
		// 设置当前页
		pagebean.setCurPage(page); // 这里page是从页面上获取的一个参数，代表页数
		// 获得分页大小
		int pagesize = pagebean.getPageSize();
		// 获得分页数据在list集合中的索引
		int firstIndex = (page - 1) * pagesize;
		int toIndex = page * pagesize;
		if (toIndex > clist.size()) {
			toIndex = clist.size();
			System.out.println(firstIndex);
			System.out.println(toIndex);
			List courseList = clist.subList(firstIndex, toIndex);
			return WebResult.OK(courseList);
		}else if (firstIndex<=0) {
			firstIndex = 0;
			toIndex=pagesize;
			List courseList = clist.subList(firstIndex, toIndex);
			return WebResult.OK(courseList);
		}else {
			List courseList = clist.subList(firstIndex, toIndex);
			return WebResult.OK(courseList);
		}
		// 截取数据集合，获得分页数据

	}
}
